<?php

namespace app\admin\controller;
use app\common\model\CourseUnit as CourseUnitModel;
use think\Db;

class CourseUnit extends Base
{

    /**
     * [index 列表]
     * @author [田建龙] [864491238@qq.com]
     */
    public function index(){

        $key = input('key');
        $map = [];
        if($key&&$key!==""){
            $map['title'] = ['like',"%" . $key . "%"];          
        }
		$course_id = input('course_id');
		$map['course_id'] = $course_id;
		$map['status'] = 1;
		
        $Nowpage = input('get.page') ? input('get.page'):1;
        $limits = config('list_rows');// 获取总条数
        $count = Db::name('course_unit')->where($map)->count();//计算总页面
        $allpage = intval(ceil($count / $limits));
        $article = new CourseUnitModel();
        $lists = $article->getDataByWhere($map, $Nowpage, $limits);
        $this->assign('Nowpage', $Nowpage); //当前页
        $this->assign('allpage', $allpage); //总页数
        $this->assign('count', $count); 
        $this->assign('val', $key);
        if(input('get.page')){
            return json($lists);
        }
        return $this->fetch('',['course_id'=>$course_id]);
    }


    /**
     * [添加]
     * @return [type] [description]
     * @author [田建龙] [864491238@qq.com]
     */
    public function add_data()
    {
        if(request()->isAjax()){

            $param = input('post.');
            $article = new CourseUnitModel();
            $flag = $article->insertData($param);
            return json(['code' => $flag['code'], 'data' => $flag['data'], 'msg' => $flag['msg']]);
        }

		$article_info = [];
		$article_info['status'] = 1;
		$this->assign('article',$article_info);
		$course_id = input('course_id');
        return $this->fetch('edit_data',['course_id'=>$course_id]);
    }


    /**
     * [编辑]
     * @return [type] [description]
     * @author [田建龙] [864491238@qq.com]
     */
    public function edit_data()
    {
        $obj = new CourseUnitModel();     
        if(request()->isAjax()){

            $param = input('post.');    
			if ($param['id']) {			
				$flag = $obj->updateData($param);
			}else{
				$flag = $obj->insertData($param);
			}
            return json(['code' => $flag['code'], 'data' => $flag['data'], 'msg' => $flag['msg']]);
        }

        $id = input('param.id');
		$info = $obj->getOneData($id);
		$this->assign('info',$info);	
        return $this->fetch('',['course_id'=>$info['course_id']]);
    }



    /**
     * [删除]
     * @return [type] [description]
     * @author [田建龙] [864491238@qq.com]
     */
    public function del_data()
    {
        $id = input('param.id');
        $cate = new CourseUnitModel();
        $flag = $cate->delData($id);
        return json(['code' => $flag['code'], 'data' => $flag['data'], 'msg' => $flag['msg']]);
    }



    /**
     * [状态]
     * @return [type] [description]
     * @author [田建龙] [864491238@qq.com]
     */
    public function data_state()
    {
        $id=input('param.id');
        $status = Db::name('course_unit')->where(array('id'=>$id))->value('status');//判断当前状态情况
        if($status==1)
        {
            $flag = Db::name('course_unit')->where(array('id'=>$id))->setField(['status'=>0]);
            return json(['code' => 1, 'data' => $flag['data'], 'msg' => '已禁止']);
        }
        else
        {
            $flag = Db::name('course_unit')->where(array('id'=>$id))->setField(['status'=>1]);
            return json(['code' => 0, 'data' => $flag['data'], 'msg' => '已开启']);
        }
    
    }



}